using AjeeleeCms.Core.Common;
using AjeeleeCms.Core.DBUtility;
using AjeeleeCms.Web.Plugin.Link.Model;
using System;
using System.Data;
using System.Data.OleDb;
using System.Text;
namespace AjeeleeCms.Web.Plugin.Link.DAL
{
	public class link
	{
		public bool Exists(int id)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("select count(1) from dt_link");
			stringBuilder.Append(" where id=@id ");
			OleDbParameter[] array = new OleDbParameter[]
			{
				new OleDbParameter("@id", OleDbType.Integer, 4)
			};
			array[0].Value = id;
			return DbHelperOleDb.Exists(stringBuilder.ToString(), array);
		}
		public int Add(AjeeleeCms.Web.Plugin.Link.Model.link model)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("insert into dt_link(");
			stringBuilder.Append("title,user_name,user_tel,email,site_url,img_url,is_image,sort_id,is_red,is_lock,add_time)");
			stringBuilder.Append(" values (");
			stringBuilder.Append("@title,@user_name,@user_tel,@email,@site_url,@img_url,@is_image,@sort_id,@is_red,@is_lock,@add_time)");
			OleDbParameter[] array = new OleDbParameter[]
			{
				new OleDbParameter("@title", OleDbType.VarChar, 255), 
				new OleDbParameter("@user_name", OleDbType.VarChar, 50), 
				new OleDbParameter("@user_tel", OleDbType.VarChar, 20), 
				new OleDbParameter("@email", OleDbType.VarChar, 50), 
				new OleDbParameter("@site_url", OleDbType.VarChar, 255), 
				new OleDbParameter("@img_url", OleDbType.VarChar, 255), 
				new OleDbParameter("@is_image", OleDbType.Integer, 4), 
				new OleDbParameter("@sort_id", OleDbType.Integer, 4), 
				new OleDbParameter("@is_red", OleDbType.Integer, 4), 
				new OleDbParameter("@is_lock", OleDbType.Integer, 4), 
				new OleDbParameter("@add_time", OleDbType.Date)
			};
			array[0].Value = model.title;
			array[1].Value = model.user_name;
			array[2].Value = model.user_tel;
			array[3].Value = model.email;
			array[4].Value = model.site_url;
			array[5].Value = model.img_url;
			array[6].Value = model.is_image;
			array[7].Value = model.sort_id;
			array[8].Value = model.is_red;
			array[9].Value = model.is_lock;
			array[10].Value = model.add_time;
			return DbHelperOleDb.ExecuteSql(stringBuilder.ToString(), array);
		}
		public void UpdateField(int id, string strValue)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("update dt_link set " + strValue);
			stringBuilder.Append(" where id=" + id);
			DbHelperOleDb.ExecuteSql(stringBuilder.ToString());
		}
		public bool Update(AjeeleeCms.Web.Plugin.Link.Model.link model)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("update dt_link set ");
			stringBuilder.Append("title=@title,");
			stringBuilder.Append("user_name=@user_name,");
			stringBuilder.Append("user_tel=@user_tel,");
			stringBuilder.Append("email=@email,");
			stringBuilder.Append("site_url=@site_url,");
			stringBuilder.Append("img_url=@img_url,");
			stringBuilder.Append("is_image=@is_image,");
			stringBuilder.Append("sort_id=@sort_id,");
			stringBuilder.Append("is_red=@is_red,");
			stringBuilder.Append("is_lock=@is_lock,");
			stringBuilder.Append("add_time=@add_time");
			stringBuilder.Append(" where id=@id");
			OleDbParameter[] array = new OleDbParameter[]
			{
				new OleDbParameter("@title", OleDbType.VarChar, 255), 
				new OleDbParameter("@user_name", OleDbType.VarChar, 50), 
				new OleDbParameter("@user_tel", OleDbType.VarChar, 20), 
				new OleDbParameter("@email", OleDbType.VarChar, 50), 
				new OleDbParameter("@site_url", OleDbType.VarChar, 255), 
				new OleDbParameter("@img_url", OleDbType.VarChar, 255), 
				new OleDbParameter("@is_image", OleDbType.Integer, 4), 
				new OleDbParameter("@sort_id", OleDbType.Integer, 4), 
				new OleDbParameter("@is_red", OleDbType.Integer, 4), 
				new OleDbParameter("@is_lock", OleDbType.Integer, 4), 
				new OleDbParameter("@add_time", OleDbType.Date), 
				new OleDbParameter("@id", OleDbType.Integer, 4)
			};
			array[0].Value = model.title;
			array[1].Value = model.user_name;
			array[2].Value = model.user_tel;
			array[3].Value = model.email;
			array[4].Value = model.site_url;
			array[5].Value = model.img_url;
			array[6].Value = model.is_image;
			array[7].Value = model.sort_id;
			array[8].Value = model.is_red;
			array[9].Value = model.is_lock;
			array[10].Value = model.add_time;
			array[11].Value = model.id;
			int num = DbHelperOleDb.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool Delete(int id)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("delete from dt_link ");
			stringBuilder.Append(" where id=@id");
			OleDbParameter[] array = new OleDbParameter[]
			{
				new OleDbParameter("@id", OleDbType.Integer, 4)
			};
			array[0].Value = id;
			int num = DbHelperOleDb.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public AjeeleeCms.Web.Plugin.Link.Model.link GetModel(int id)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("select  top 1 id,title,user_name,user_tel,email,site_url,img_url,is_image,sort_id,is_red,is_lock,add_time from dt_link ");
			stringBuilder.Append(" where id=@id");
			OleDbParameter[] array = new OleDbParameter[]
			{
				new OleDbParameter("@id", OleDbType.Integer, 4)
			};
			array[0].Value = id;
			AjeeleeCms.Web.Plugin.Link.Model.link link = new AjeeleeCms.Web.Plugin.Link.Model.link();
			DataSet dataSet = DbHelperOleDb.Query(stringBuilder.ToString(), array);
			AjeeleeCms.Web.Plugin.Link.Model.link result;
			if (dataSet.Tables[0].Rows.Count > 0)
			{
				if (dataSet.Tables[0].Rows[0]["id"] != null && dataSet.Tables[0].Rows[0]["id"].ToString() != "")
				{
					link.id = int.Parse(dataSet.Tables[0].Rows[0]["id"].ToString());
				}
				if (dataSet.Tables[0].Rows[0]["title"] != null && dataSet.Tables[0].Rows[0]["title"].ToString() != "")
				{
					link.title = dataSet.Tables[0].Rows[0]["title"].ToString();
				}
				if (dataSet.Tables[0].Rows[0]["user_name"] != null && dataSet.Tables[0].Rows[0]["user_name"].ToString() != "")
				{
					link.user_name = dataSet.Tables[0].Rows[0]["user_name"].ToString();
				}
				if (dataSet.Tables[0].Rows[0]["user_tel"] != null && dataSet.Tables[0].Rows[0]["user_tel"].ToString() != "")
				{
					link.user_tel = dataSet.Tables[0].Rows[0]["user_tel"].ToString();
				}
				if (dataSet.Tables[0].Rows[0]["email"] != null && dataSet.Tables[0].Rows[0]["email"].ToString() != "")
				{
					link.email = dataSet.Tables[0].Rows[0]["email"].ToString();
				}
				if (dataSet.Tables[0].Rows[0]["site_url"] != null && dataSet.Tables[0].Rows[0]["site_url"].ToString() != "")
				{
					link.site_url = dataSet.Tables[0].Rows[0]["site_url"].ToString();
				}
				if (dataSet.Tables[0].Rows[0]["img_url"] != null && dataSet.Tables[0].Rows[0]["img_url"].ToString() != "")
				{
					link.img_url = dataSet.Tables[0].Rows[0]["img_url"].ToString();
				}
				if (dataSet.Tables[0].Rows[0]["is_image"] != null && dataSet.Tables[0].Rows[0]["is_image"].ToString() != "")
				{
					link.is_image = int.Parse(dataSet.Tables[0].Rows[0]["is_image"].ToString());
				}
				if (dataSet.Tables[0].Rows[0]["sort_id"] != null && dataSet.Tables[0].Rows[0]["sort_id"].ToString() != "")
				{
					link.sort_id = int.Parse(dataSet.Tables[0].Rows[0]["sort_id"].ToString());
				}
				if (dataSet.Tables[0].Rows[0]["is_red"] != null && dataSet.Tables[0].Rows[0]["is_red"].ToString() != "")
				{
					link.is_red = int.Parse(dataSet.Tables[0].Rows[0]["is_red"].ToString());
				}
				if (dataSet.Tables[0].Rows[0]["is_lock"] != null && dataSet.Tables[0].Rows[0]["is_lock"].ToString() != "")
				{
					link.is_lock = int.Parse(dataSet.Tables[0].Rows[0]["is_lock"].ToString());
				}
				if (dataSet.Tables[0].Rows[0]["add_time"] != null && dataSet.Tables[0].Rows[0]["add_time"].ToString() != "")
				{
					link.add_time = DateTime.Parse(dataSet.Tables[0].Rows[0]["add_time"].ToString());
				}
				result = link;
			}
			else
			{
				result = null;
			}
			return result;
		}
		public DataSet GetList(string strWhere)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("select id,title,user_name,user_tel,email,site_url,img_url,is_image,sort_id,is_red,is_lock,add_time ");
			stringBuilder.Append(" FROM dt_link ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			return DbHelperOleDb.Query(stringBuilder.ToString());
		}
		public DataSet GetList(int Top, string strWhere)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("select ");
			if (Top > 0)
			{
				stringBuilder.Append(" top " + Top.ToString());
			}
			stringBuilder.Append(" id,title,user_name,user_tel,email,site_url,img_url,is_image,sort_id,is_red,is_lock,add_time ");
			stringBuilder.Append(" FROM dt_link ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			stringBuilder.Append(" order by sort_id asc,add_time desc");
			return DbHelperOleDb.Query(stringBuilder.ToString());
		}
		public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("select * FROM dt_link");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			recordCount = Convert.ToInt32(DbHelperOleDb.GetSingle(PagingHelper.CreateCountingSql(stringBuilder.ToString())));
			return DbHelperOleDb.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, stringBuilder.ToString(), filedOrder));
		}
	}
}
